package org.store.rest;

import org.springframework.beans.factory.annotation.Autowired;
import org.store.domain.Category;
import org.store.service.CategoriesHolder;

import javax.ws.rs.*;
import java.util.Collection;

@Path("/category")
@Produces("application/json")
public class CategoriesResource {

    @Autowired
    private CategoriesHolder holder;

    @GET
    public Collection<Category> getAll() throws Exception {
        return holder.getParentCategories();
    }

    @GET
    @Path("/{id}")
    public Collection<Category> getAllSubcategories(@PathParam("id") Long categoryId) throws Exception {
        Category category = holder.getCategories().get(categoryId);
        if (category == null){
            throw new NotFoundException();
        }
        return category.getSubcategories();
    }
}